package com.lt.flashcard.model.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.Reference;
import com.lt.flashcard.Utility;

/**
 * The product code information for flashcardset
 * @author ocsen
 *
 */
@Entity(value="productcode",noClassnameStored=true)
public class ProductCode {

	@Id
	private ObjectId id;
	
	@Indexed(unique=true)
	private String code; //The unique code
	
	private Date created = new Date();
	
	private int status = STATUS_NEW; //The status
	public static final int STATUS_NEW = 0; //new code, not used yet
	public static final int STATUS_USED = 1; //code has been used
	public static final int STATUS_EXPIRED = 2; //code has been expired
	public static final int STATUS_ACTIVE = 3; //code has been used and still valid
	
	private int type = TYPE_NORMAL; //The type of this code
	public static final int TYPE_NORMAL = 0; //Normal code
	public static final int TYPE_PROMOTE = 1; //Promote code
	public static final int TYPE_MULTI = 2; //Code can be use by multi user
	
	private Date expireOn = null; //This code will be expired on this date, null for no expire
	
	private int maxUse = 1;  //the maximum number for use
	private int currentUse = 0;
	
	private String note = "";
	
	private Map<Long, String> products = new HashMap<Long, String>();
	
	private Date useOn = null; //The date this code is used
	private Long useByUserId = null; //The userid that use this code, or the first person that use this code
	private String useByUserName = null;
	private String useIp = null;
	private List<ProductCodeHistory> useHistory = new ArrayList<ProductCodeHistory>();
	
	private String partId = ""; //Store the part id number
	/**
	 * This code is for
	 */
	@Reference
	private Partner partner = null;
	
	public ObjectId getId() {
		return id;
	}
	
	public void setId(ObjectId id) {
		this.id = id;
	}
	
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public Date getCreated() {
		return created;
	}
	
	public void setCreated(Date created) {
		this.created = created;
	}
	
	public int getStatus() {
		return status;
	}
	
	public void setStatus(int status) {
		this.status = status;
	}
	
	public int getType() {
		return type;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
	public Date getExpireOn() {
		return expireOn;
	}
	
	public void setExpireOn(Date expireOn) {
		this.expireOn = expireOn;
	}
	
	public int getMaxUse() {
		return maxUse;
	}
	
	public void setMaxUse(int maxUse) {
		this.maxUse = maxUse;
	}
	
	public int getCurrentUse() {
		return currentUse;
	}
	
	public void setCurrentUse(int currentUse) {
		this.currentUse = currentUse;
	}
	
	public String getNote() {
		return note;
	}
	
	public void setNote(String note) {
		this.note = note;
	}
	
	public Date getUseOn() {
		return useOn;
	}
	
	public void setUseOn(Date useOn) {
		this.useOn = useOn;
	}
	
	public Long getUseByUserId() {
		return useByUserId;
	}
	
	public void setUseByUserId(Long useByUserId) {
		this.useByUserId = useByUserId;
	}
	
	public String getUseByUserName() {
		return useByUserName;
	}
	
	public void setUseByUserName(String useByUserName) {
		this.useByUserName = useByUserName;
	}
	
	public String getUseIp() {
		return useIp;
	}
	
	public void setUseIp(String useIp) {
		this.useIp = useIp;
	}
	
	public List<ProductCodeHistory> getUseHistory() {
		return useHistory;
	}
	
	public void addUseHistory(ProductCodeHistory history) {
		if(history != null)
			this.useHistory.add(history);
		
		currentUse += 1;
		if(currentUse >= maxUse)
			status = STATUS_USED;
		else
			status = STATUS_ACTIVE;
		
		useOn = history.getUsedOn();
		useByUserName = history.getUserName();
		useIp = history.getUsedIp();
		useByUserId = history.getUserId();
	}
	
	public void setUseHistory(List<ProductCodeHistory> useHistory) {
		this.useHistory = useHistory;
	}

	/**
	 * @return the partId
	 */
	public String getPartId() {
		return partId;
	}

	/**
	 * @param partId the partId to set
	 */
	public void setPartId(String partId) {
		this.partId = partId;
	}

	/**
	 * @return the partner
	 */
	public Partner getPartner() {
		return partner;
	}

	/**
	 * @param partner the partner to set
	 */
	public void setPartner(Partner partner) {
		this.partner = partner;
	}
	
	/**
	 * Add a product to this code. This will be active when code is purchased
	 * @param productid
	 * @param productname
	 */
	public void addProduct(Long productid, String productname) {
		if(productid == null || productid == 0l || Utility.isEmptyString(productname))
			return;
		
		products.put(productid, productname);
	}

	/**
	 * @return the products
	 */
	public Map<Long, String> getProducts() {
		return products;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(Map<Long, String> products) {
		this.products = products;
	}
}
